﻿using lsw_VoteSys.Models.Database;
using lsw_VoteSys.service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace lsw_VoteSys.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProjectController : ControllerBase
    {
        private readonly lsw_VoteSysContext _db;

        public ProjectController(lsw_VoteSysContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 上传列表
        /// </summary>
        [HttpPost]
        [Authorize] 
        public IActionResult UploadProject([FromBody] ProjectModel projectModel)
        {
            if (projectModel == null)
            {
                return BadRequest("上传的项目信息不能为空");
            }

            // 创建项目实体
            var project = new Project
            {
                Title = projectModel.Title,
            };

            _db.Projects.Add(project);
            var saveResult = _db.SaveChanges();

            if (saveResult > 0)
            {
                return Ok("项目上传成功，等待审核");
            }

            return StatusCode(500, "项目保存失败，请稍后重试");
        }

        private string ConvertToHexString(byte[] bytes)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                builder.Append(bytes[i].ToString("X2"));
            }
            return builder.ToString();
        }
    }

    public class ProjectModel
    {
        public string Title { get; set; }
        public string ProjectsDatil { get; set; }
        public int ProjectID { get; set; }
    }
}
